package com.docMan.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.docMan.bean.Page;
import com.docMan.dao.BaseDao;
import com.docMan.service.BaseService;
import com.docMan.utils.ProjectUtils;

public class BaseServiceImpl<T> implements BaseService<T> {
	
	private Map<String, Object> result=new HashMap<String, Object>();
	private BaseDao<T> dao;
	
	protected void put(String key,Object value){
		result.put(key, value);
	}
	/**
	 * 在返回的map中设置‘success’的值为true，表示运行成功
	 */
	protected void putSuccess(){
		result.put("success", true);
	}
	/**
	 * 在返回的map中设置‘success’的值为false，表示运行成功
	 */
	protected void putFalse(){
		result.put("success", false);
	}
	protected void putMessage(String message){
		result.put("message", message);
	}
	protected void clear(){
		result.clear();
	}
	/**
	 * 将结果放在map中，并将map json序列化返回string
	 * @return
	 */
	protected String getMap(){
		return ProjectUtils.gson.toJson(result);
	}
	
	public BaseDao<T> getDao() {
		return dao;
	}
	
	public void setDao(BaseDao<T> dao) {
		this.dao = dao;
	}

	@Override
	public void delete(int id) {
		dao.delete(id);
	}
	
	@Override
	public T getById(Serializable id) {
		return dao.getById(id);
	}

	@Override
	public List<T> findAll(String hql) {
		return dao.findAll(ProjectUtils.getMemcachedKey(hql));
	}
	
	@Override
	public List<T> findByIds(String[] ids) {
		return dao.findByIds(ids);
	}

	@Override
	public int save(T entity) {
		return dao.save(entity);
	}
	
	@Override
	public void update(T entity) {
		dao.update(entity);
	}

	@Override
	public List<T> find( String hql) {
		
		return dao.find(ProjectUtils.getMemcachedKey(hql),hql);
	}

	@Override
	public List<T> find(String hql, Object... objects) {
		
		return dao.find(ProjectUtils.getMemcachedKey(hql, objects),hql, objects);
	}

	@Override
	public List<T> query(String hql) {
		return dao.query(hql);
	}

	@Override
	public List<T> query(String hql, int page, int pageSize) {
		return dao.query(hql, page, pageSize);
	}

	@Override
	public List<T> query(String hql, Object... objects) {
		return dao.query(hql, objects);
	}

	@Override
	public List<T> query(String hql, int page, int pageSize, Object[] objects) {
		return dao.query(hql, page, pageSize, objects);
	}

	@Override
	public List<T> createCriteria(int page, int pageSize,
			Map<String, Object> map) {
		return dao.createCriteria(page, pageSize, map);
	}

	@Override
	public List<T> createCriteria(Map<String, Object> map) {
		return dao.createCriteria(map);
	}

	@Override
	public int excudeSqlUpdate(String sql, Object... objects) {
		return dao.excudeSqlUpdate(sql, objects);
	}

	@Override
	public int update(String hql, Object... objects) {
		return dao.update(hql, objects);
	}

	@Override
	public List<T> pageQuery(String hql, int page, int pageSize,
			Object... objects) {
		return dao.pageQuery(hql, page, pageSize, objects);
	}

	@Override
	public Page<T> page(String hql,Page<T> page) {
		return dao.page(hql, page);
	}

	@Override
	public int update(String hql) {
		return dao.update(hql);
	}

	@Override
	public int excudeSqlUpdate(String sql) {
		return dao.excudeSql(sql);
	}

	@Override
	public void saveOrUpdateAll(List<T> list) {
		dao.saveOrUpdateAll(list);
	}

	@Override
	public void saveOrUpdate(T entity) {
		dao.saveOrUpdate(entity);
	}

	@Override
	public void delete(T entity) {
		dao.delete(entity);
	}

	@Override
	public List<T> find(String hql, List<String> params) {
		return dao.find(hql, params);
	}

	@Override
	public long count(String hql, Object... objects) {
		return dao.count(hql, objects);
	}

	@Override
	public List<T> find(String hql, List<String> classes, List<String> params) {
		return dao.find(hql, classes, params);
	}

	@Override
	public Page<T> restPage(String hql, Page<T> page) {
		String key=ProjectUtils.getMemcachedKey(hql, page.getCurrentPage(),page.getParams());
		return dao.restPage(key,hql, page);
	}

	@Override
	public List<T> findNoCache(String hql) {
		return dao.findNoCache(hql);
	}

	@Override
	public List<T> findNoCache(String hql, Object... objects) {
		return dao.findNoCache(hql, objects);
	}

	@Override
	public Page<T> restPageNoCache(String hql, Page<T> page) {
		return dao.restPageNoCache(hql, page);
	}
	@Override
	public List sqlQueryNoCache(String sql, Object... objects) {
		
		return dao.findSqlNoCache(sql, objects);
	}
	@Override
	public List sqlQueryNoCache(String sql) {
		return dao.findSqlNoCache(sql);
	}
	@Override
	public List<List> findList(String hqlString) {
		return dao.find(hqlString);
	}
	@Override
	public Page<T> newPage(Page<T> page, Query query) {
		return dao.newPage(page, query);
	}
	@Override
	public List<List> findList(Query query) {
		return dao.find(query);
	}
	@Override
	public Query getQuery(String hql) {
		return dao.getQuery(hql);
	}

	
}
